package com.lepu.app.main.activity;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.app.config.AppConfig;
import com.app.utils.Const;
import com.app.utils.Setting;
import com.app.utils.Utils;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.BrowserLocaljsObject.BrowserLocaljsObjectDelegate;
import com.core.lib.utils.BrowserWebChromeClient;
import com.core.lib.utils.BrowserWebChromeClient.BrowserWebChromeDelegate;
import com.core.lib.utils.BrowserWebClient;
import com.core.lib.utils.BrowserWebClient.BrowserWebClientDelegate;
import com.core.lib.widget.CustomWebView;
import com.eyzhs.app.R;
import com.lepu.app.usercenter.LoginActivity;
import com.lepu.app.usercenter.RegisterActivity;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

public class AppEnterWebGuideActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener,
		BrowserWebChromeDelegate,BrowserWebClientDelegate,BrowserLocaljsObjectDelegate {

	private static final String GOTO_INDEX = "goto::skip";
	private static final String GOTO_REGISTER = "goto::register";
	private static final String GOTO_LOGIN = "goto::login";

	private CustomWebView mWebView = null;
	private ProgressBar mProgressBar = null;

	private CustomTopBarNew mTopbar = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		mNeedSwipeBack = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_enter_web_guide_activity);

		init();
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	public void onResume() {
		mWebView.onResume();
		super.onResume();
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	public void onPause() {
		mWebView.onPause();
		super.onPause();
	}

	@Override
	public void onDestroy() {
		mWebView.destroy();

		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {

	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		gotoEvent(0);
	}

	/**
	 * 初始化方法
	 */
	@SuppressLint("SetJavaScriptEnabled")
	private void init() {
		// topbar
		mTopbar = (CustomTopBarNew) findViewById(R.id.topbar);
		mTopbar.setTopbarTitle("");
		mTopbar.setonTopbarNewLeftLayoutListener(this);
		mTopbar.setTopbarLeftLayout(R.drawable.ship_webview_close, 0, 0);
		
		//值为不是第一次使用
		AppConfig.setConfig(this, Const.CONFIG_APP_FIRST_USE, false);
		AppConfig.setConfig(this, Const.CONFIG_APP_UPGRADE_USE, false);

		mProgressBar = (ProgressBar) findViewById(R.id.topProgressBar);

		mWebView = (CustomWebView) findViewById(R.id.webView);
		mWebView.setWebChromeClient(new BrowserWebChromeClient(this));
		mWebView.setWebViewClient(new BrowserWebClient(this,this,mWebView));

		Utils.initWebViewSettings(mWebView,false);

		mWebView.loadUrl(Setting.getRegisterWebGuideUrl());
	}

	public void onProgressChanged(WebView view, int newProgress) {
		// LogUtilBase.LogD(null, "onProgressChanged===>>" + newProgress);
		mProgressBar.setMax(100);
		if (newProgress < 100) {
			if (mProgressBar.getVisibility() == View.GONE) {
				mProgressBar.setVisibility(View.VISIBLE);
			}
			mProgressBar.setProgress(newProgress);
		} else {
			mProgressBar.setProgress(100);

			Animation animation = AnimationUtils.loadAnimation(this,
					R.anim.lib_broswer_progress);
			// 运行动画
			mProgressBar.startAnimation(animation);
			// 将 spinner 的可见性设置为不可见状态
			mProgressBar.setVisibility(View.GONE);
		}
	}
	
	private void gotoEvent(int index)
	{
		finishAnim(0,0);
		
		Intent intent = new Intent();
		switch (index) {
		case 0:
			
			intent.setClass(this, MainTabActivity.class);
			startActivityAnim(intent, 0,0);
			break;
		case 1:
			
			intent.setClass(this, RegisterActivity.class);
			intent.putExtra("IsPromotion", 1);
			startActivityAnim(intent, 0,0);
			break;
		case 2:
			
			intent.setClass(this, LoginActivity.class);
			startActivityAnim(intent, 0,0);
			break;

		default:
			break;
		}
	}

	// ======================================================
	@Override
	public void onPageStarted(String arg0) {
		
	}

	@Override
	public void onPageFinished(String arg0) {
		
	}

	@Override
	public boolean shouldOverrideUrlLoading(WebView webView, String url) {
		if (url.contains(GOTO_INDEX)) {
			// 跳main
			gotoEvent(0);
		} else if (url.contains(GOTO_REGISTER)) {
			// 跳注册
			gotoEvent(1);
		} else if (url.contains(GOTO_LOGIN)) {
			// 跳login
			gotoEvent(2);
		} else {
			webView.loadUrl(url);
		}
		return true;
	}

	@Override
	public void getCover(String arg0) {
		
	}

	@Override
	public void getDescription(String arg0) {
		
	}

	@Override
	public void getHtmlLength(int arg0) {
		
	}

	@Override
	public void getReferrer(String arg0) {
		
	}

	@Override
	public void getTitle(final String title) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mTopbar.setTopbarTitle(title);
			}
		});

	}

	@Override
	public void getVideoSrc(String arg0) {
		
	}

	@Override
	public void getArticleID(String arg0) {
		
	}

}
